package sc.display;

import java.util.List;
import java.util.Map;

import javax.swing.table.AbstractTableModel;

import sc.writable.IntermediateData;
import sc.writable.StockSummary;

/**
 * Model of the main table in the main frame. It stores the data.
 * 
 * @author Quentin Rossé
 *
 */
@SuppressWarnings("serial")
public class MainTableModel extends AbstractTableModel{

	private List<StockSummary> stocks;
	
	private Map<String, List<IntermediateData>> stockDetails;
	
	private Object[][] tableData;
	
	private String[] columnNames;
	
	
	public MainTableModel(String[] columnNames, 
			              List<StockSummary> stocks,
			              Map<String, List<IntermediateData>> stockDetails){
		this.columnNames = columnNames;
		this.stocks = stocks;
		this.stockDetails = stockDetails;
	}
	
	@Override
	public String getColumnName(int col) {
        return columnNames[col];
    }
	
	
	@Override
	public int getColumnCount() {
		return columnNames.length;
	}

	@Override
	public int getRowCount() {
		return tableData.length;
	}

	@Override
	public Object getValueAt(int i, int j) {
		return tableData[i][j];
	}
	
	 @Override
     public Class<?> getColumnClass(int columnIndex){
         if( columnIndex == 0){
            return String.class;
         }else{
        	 return Integer.class;
         }
     }
	
	
	public void updateTableData(){
		tableData = new Object[stocks.size()][columnNames.length];
		StockSummary ss;
		int[] score;
		
		for(int i=0; i<stocks.size(); i++){
			ss = stocks.get(i);
			System.out.println(ss.getStock());
			tableData[i][0] = ss.getStock();
			score = ss.getScore();
			for(int j=0; j<score.length; j++){
				tableData[i][j+1] = score[j];
			}
		}
	}
	
	/**
	 * Open window which shows the detailed information about the stock
	 * 
	 * @param row the row of the stock /!\ it's the model row, so be careful
	 *            your table can be sorted, filtered or whatever
	 */
	public void openDetails(int row){
		System.out.println(tableData[row][0]);
		List<IntermediateData> details = stockDetails.get(tableData[row][0]);
		Object[][] detailTableData = new Object[details.size()][4];
		String[] detail;
		
		for(int i=0; i<details.size(); i++){
			detail = details.get(i).toStringArray();
			for(int j=0; j<detail.length; j++)
				detailTableData[i][j] = detail[j];
		}
		
		new DetailFrame(tableData[row][0].toString(), detailTableData);
	}

}
